export function TodoFormController($scope, $rootScope) {
  //当前组件声明属性和方法
  $scope.todoText = '';
  $scope.addTodo = () => {
    const todoText = $scope.todoText;
    if (!todoText.length) return;
    //访问其他组件定义的公共属性
    $rootScope.todoData.push({
      id: new Date().getTime(),
      content: $scope.todoText,
      completed: false
    });
    $scope.todoText = '';
  };
}

export function TodoListController($scope, $rootScope) {
  //定义公共的属性给多个视图模板使用
  $rootScope.todoData = [];
  $scope.toggleTodo = (id) => {
    const todoData = $rootScope.todoData;
    $rootScope.todoData = todoData.map((todo) => {
      if (todo.id === id) {
        todo.completed = !todo.completed;
      }
      return todo;
    });
  };
  $scope.removeTodo = (id) => {
    const todoData = $rootScope.todoData;
    $rootScope.todoData = todoData.filter((todo) => todo.id !== id);
  };
}
